package com.wwl.io;

import java.io.*;

public class FileCopyCompare {

    public static void main(String[] args) throws IOException {

        String fileName="data/yttlj.txt";
        String fileName2="data/yttlj2.txt";
        String fileName3="data/yttlj3.txt";

        long start1=System.currentTimeMillis();
        copyFileByFilkeReaderAndFileWriter(fileName,fileName2);
        long end1=System.currentTimeMillis();


        long start2=System.currentTimeMillis();
        copyFileByBufferedReaderAndBufferedWriter(fileName,fileName3);
        long end2=System.currentTimeMillis();

        System.out.println("FileReaderAndWriter=="+(end1-start1));
        System.out.println("BufferedReaderAndWriter=="+(end2-start2));

    }

    public static void copyFileByFilkeReaderAndFileWriter(String fileName,String fileName2) throws IOException {

        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader(fileName);
            fw = new FileWriter(fileName2);
            int ch = fr.read();
            while(ch != -1){
                fw.write(ch);
                ch = fr.read();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fr.close();
            fw.close();
        }

    }

    public static void copyFileByBufferedReaderAndBufferedWriter(String fileName,String fileName2){

        try {
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileName2));
            String line = br.readLine();
            while(line!=null){
                bw.write(line);
                bw.newLine();
                line=br.readLine();
            }
            br.close();
            bw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
